<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/2/11
 * Time: 14:00
 */

namespace app\base\service;
use think\Db;
use Wechat;
use think\Hook;
class WxNotify extends Wechat\WxPayNotify
{
    public function Queryorder($transaction_id)
    {
        $input = new Wechat\WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = Wechat\WxPayApi::orderQuery($input);
        if (array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS"
        ) {
            return true;
        }
        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        if (!array_key_exists("transaction_id", $data)) {
            $msg = "输入参数不正确";
            trace($msg,'wxPay');
            return false;
        }
        //查询订单，判断订单真实性
        if (!$this->Queryorder($data["transaction_id"])) {
            $msg = "订单查询失败";
            trace($msg,'wxPay');
             return false;
        }
        $money    = $data['total_fee'];                  //金额
        $order_no = $data['out_trade_no'];            //订单编号
        //充值订单查询
        $recharge_where['rrd.recharge_no'] = $order_no;
        $now_date = date('Y-m-d',time());
        //$recharge_where['at.begin_date'] = array('elt',$now_date);
        //$recharge_where['at.end_date'] = array('egt',$now_date);
        $recharge_info = Db::name('recharge_record')->alias('rrd')
            //关联充值优惠
            ->join('activity at','rrd.area_id = at.area_id','left')
            ->field('rrd.passenger_id,rrd.recharge_record_id,rrd.recharge_no,rrd.is_success,at.recharge_price,at.give_price,at.begin_date,at.end_date')
            ->where($recharge_where)->find();

        if ($recharge_info) {
            if ($recharge_info['is_success'] == '9' && is_numeric($money)) {
                $dataWhere = $recharge_info['begin_date']<= $now_date && $recharge_info['end_date']>=$now_date;
                if($recharge_info['recharge_price'] && $money >= $recharge_info['recharge_price'] && $dataWhere){
                    //充值金额加上赠送金额
                    $give_price = $recharge_info['give_price'];
                    $re_data['give_money'] = $give_price;
                    $money = $money + $give_price;
                }
                $re_data['is_success'] = '1';
                Db::name('recharge_record')->where('recharge_no',$order_no)->update($re_data);
                Db::name('passenger')->where('passenger_id',$recharge_info['passenger_id'])->Inc('balance',$money)->update();
                trace('充值成功','wxPay');
                return true;//充值成功
            }
            trace('充值失败'.$recharge_info['recharge_record_id'],'wxPay');
        };
        //用车订单完成 -- 支付完成
        $order_info = Db::name('order')->where('order_no',$order_no)->find();
        if ($order_info) {
            $oid = $order_info['order_id'];
            trace($order_info['order_no'],'wxPay');
            if ($order_info['order_state'] == '9' && is_numeric($money)) {
                $updateData['payment_time'] = date('Y-m-d H:i:s');
                $updateData['order_state']  = 10;
                Db::name('order')->where('order_no',$order_no)->update($updateData);
                Hook::listen('pay_order',$oid);
                trace('支付成功','wxPay');
                return true;
            }
        }
        trace('支付失败','wxPay');
        $success = false;
        return $success;//是否支付成功
    }

    //储存微信支付回传数据by yulin
    public function check_wxpay($data) {
        $data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if($data['return_code'] && $data['return_msg']) {
            Db::name('company_recharge')->where('company_recharge_id', $data['attach'])->update(['state' => '3']);
            return true;
        }
    }
}